package com.calligraphy.order.controller;

import com.calligraphy.order.dto.OrderDTO;
import com.calligraphy.order.message.StreamClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author hy
 * @Date 2019/1/8 9:57
 **/
@RestController
public class MqController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StreamClient streamClient;

    /**
     * RabbitMq
     */
    @RequestMapping("/mqSet")
    public void mqSet(){
        amqpTemplate.convertAndSend("myOrder","myKey","now"+System.currentTimeMillis());
    }

    /**
     * Stream 发送端(生产消息)
     * @return
     */
    @RequestMapping("/sendMsg")
    public String msg(){
        String msg = "now"+ new Date();
        streamClient.output().send(MessageBuilder.withPayload(msg).build());
        return "msg";
    }

    /**
     * Stream 发送端(生产消息)
     * @return
     */
    @RequestMapping("/process")
    public String process(){
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderId("123456");
        streamClient.output().send(MessageBuilder.withPayload(orderDTO).build());
        return null;
    }
}
